package com.wchen.gdoc.generate.processor;

import com.alibaba.fastjson.JSONObject;
import com.wchen.gdoc.generate.config.ApplicationConfigProvide;
import com.wchen.gdoc.generate.mapping.data.ClassData;
import com.wchen.gdoc.generate.mapping.data.MethodData;
import com.wchen.gdoc.generate.util.FreemarkerTemplateUtil;
import com.wchen.gdoc.generate.util.StringTemplateUtil;
import com.wchen.gdoc.generate.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;


/**
 * 默认数据处理器
 *
 * @author  wchen
 * @date    2019/11/11
 */
public class DefaultDataProcessor implements DataProcessor {
    private final static Logger LOGGER = LoggerFactory.getLogger(DefaultDataProcessor.class);
    private DocumentSqlManager documentSqlManager = new DocumentSqlManager();

    /**
     * 处理classData数据
     *
     * @param classData
     */
    @Override
    public void dealClassData(ClassData classData) {
        String classCommentText = classData.getClassCommentText();
        String projectId = documentSqlManager.getProjectId();
        JSONObject sqlParams = new JSONObject();
        sqlParams.put("projectId", projectId);
        String moduleId = documentSqlManager.getMouldId(sqlParams);
        String classId = getClassId(classCommentText, projectId, moduleId);

        List<MethodData> methodDataList = classData.getMethodDataList();
        methodDataList.forEach(methodData -> dealMethodData(methodData, projectId, moduleId, classId));
    }

    /**
     * 处理methodData
     *
     * @param methodData
     * @param projectId
     * @param moduleId
     * @param classId
     */
    public void dealMethodData(MethodData methodData, String projectId, String moduleId, String classId) {

        JSONObject sqlParam = new JSONObject();
        sqlParam.put("projectId", projectId);
        sqlParam.put("moduleId", moduleId);
        sqlParam.put("classId", classId);
        sqlParam.put("methodCommentText", methodData.getMethodCommentText());

        String methodId = documentSqlManager.getMethodId(sqlParam);
        if(StringUtil.isNotBlank(methodId)) {
            LOGGER.info(String.format("%s is already exists!", methodData.getMethodCommentText()));
            return;
        }

        //通过模板构建参数
        JSONObject methodDataParam = JSONObject.parseObject(JSONObject.toJSONString(methodData));
        String content = FreemarkerTemplateUtil.generateContent(methodDataParam, ApplicationConfigProvide.getApplicationConfig().getDocumentConfig().getContentTemplatePath());
        methodDataParam.putAll(sqlParam);
        methodDataParam.put("id", StringTemplateUtil.unionId());
        methodDataParam.put("content", content);
        Boolean executeResult = documentSqlManager.saveMethodInfo(methodDataParam);
        LOGGER.info(String.format("%s : %s  save result:%s", methodData.getMethodCommentText(), methodData.getRequestUrl(), executeResult));
    }


    /**
     * 获取classId
     *
     * @param classCommentText
     * @param projectId
     * @param moduleId
     * @return
     */
    public String getClassId(String classCommentText, String projectId, String moduleId) {
        JSONObject sqlParam = new JSONObject();
        sqlParam.put("projectId", projectId);
        sqlParam.put("moduleId", moduleId);
        sqlParam.put("classCommentText", classCommentText);
        String classId = documentSqlManager.getClassId(sqlParam);
        if(StringUtil.isNotBlank(classId)) {
            return classId;
        }

        classId = StringTemplateUtil.unionId();
        sqlParam.put("classCommentText", classCommentText);
        sqlParam.put("id", classId);
        Boolean executeFlag = documentSqlManager.saveClassInfo(sqlParam);
        if(executeFlag) {
            return classId;
        }
        throw new RuntimeException("getClassId error");
    }

}
